<template>
  <div class="warp-box" id="upadd">
    <el-row class="search-box">
      <el-col :span="24" class="add-bottom">
        <span class="personal-title" v-text="parames.id?'编辑应用':'新建应用'"></span>
      </el-col>
    </el-row>
    <div class="main-box">
      <div class="inforlist">
        <div class="inforadd" style="margin-top: 30px;width: 880px">
          <el-form
            v-loading="loading"
            :model="parames"
            :rules="rules"
            ref="parames"
            label-width="120px"
            class="demo-ruleForm"
          >
            <el-form-item label="名称:" prop="name">
              <el-input style="width:400px;" v-model="parames.name" placeholder="请输入名称" @input="changeValue"></el-input>
            </el-form-item>
            <el-form-item label="boundleId:" prop="boundleId">
              <el-input style="width:400px;" v-model="parames.boundleId" placeholder="请输入boundleId" @input="changeValue"></el-input>
            </el-form-item>
            <el-form-item label="渠道:" prop="channel">
              <el-select style="width:400px;" v-model="parames.channel" placeholder="请选择渠道" @change="changeValue">
                <el-option
                  v-for="item in channelData"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="审核版本:" prop="version">
              <el-input style="width:400px;" v-model="parames.version" placeholder="请输入审核版本" @input="changeValue"></el-input>
            </el-form-item>
            <el-form-item label="链接:" prop="url">
              <el-input
                style="width:400px;float:left"
                v-model="parames.url"
                placeholder="请输入应用下载地址"
              ></el-input>
              <el-upload
                v-show="!uploadShow"
                style="float:left"
                class="upload-demo"
                :accept="accept"
                name="file"
                :before-upload="beforeUpload"
                :data="uploadData"
                :action="action"
                :headers="headers"
                :on-success="uploadimg"
                :on-progress="onUpload"
                :on-error="onError"
                :show-file-list="false"
              >
                <span  class="search-btn backorgin" style="margin: 0 15px">
                  <i  class="element-icons icon-shangchuan" style="font-size:20px;"></i>
                </span>
              </el-upload>
                <span v-show="uploadShow" class="search-btn backorgin" style="margin: 0 15px;display:inline-block" @click="goBtn">
                  <i  class="element-icons icon-shangchuan" style="font-size:20px;"></i>
                </span>
            </el-form-item>
            <el-form-item label="状态:" prop="status">
              <el-popover
                placement="right"
                title
                offset="300"
                width="200"
                trigger="manual"
                v-model="statusBtn"
                content="温馨提示：选择下线后应用就不会显示在前端界面上！"
              ></el-popover>
              <el-switch
                v-model="parames.status"
                active-value="1"
                inactive-value="0"
                active-text="上线"
                inactive-text="下线"
                @change="changeStatus"
              ></el-switch>
            </el-form-item>

            <el-form-item label="简介:" prop="memo">
              <el-input
                rows="6"
                style="width:400px;"
                type="textarea"
                v-model="parames.memo"
                placeholder="请输入简介"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button round type="primary" @click="handleSend">保存并返回列表</el-button>
              <el-button round @click="resetData">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      parames: {
        id: "",
        name: "",
        url: "",
        ios: "",
        channel: "",
        boundleId: "",
        version: "",
        status: "0",
        memo: ""
      },
      uploadShow: true,
      loading: false,
      accept: '',
      uploadData: {
        boundleId: "",
        ios: "",
        version: "",
        showName: ""
      },
      statusBtn: false,
      channelData: [
        { value: "IOSOfficial", label: "苹果非应用市场" },
        { value: "AndroidOfficial", label: "安卓非应用市场" },
        { value: "AppStore", label: "苹果" },
        { value: "Xiaomi", label: "小米" },
        { value: "Baidu", label: "百度" },
        { value: "Huawei", label: "华为" },
        { value: "Vivo", label: "Vivo" },
        { value: "Oppo", label: "Oppo" }
      ],
      action: "",
      headers: {
        "X-Requested-With": "XMLHttpRequest"
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        url: [{ required: true, message: "请输入链接", trigger: "blur" }],
        version: [{ required: true, message: "请输入版本", trigger: "blur" }],
        boundleId: [{ required: true, message: "请输入版本", trigger: "blur" }],

        channel: [
          { required: true, message: "请选择渠道", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    uploadimg(response) {
      this.parames.url = response.data
      this.loading = false
        if(response.code !== 10200) {
            this.$alert(response.msg, '提示')
        }
    },
    onError (err) {
        this.loading = false
        this.$alert('文件上传失败', '提示')
    },
    onUpload () {
        this.loading = true
    },
    changeValue (val) {
      if (this.parames.name !== '' && this.parames.boundleId !== '' && this.parames.channel !== '' && this.parames.version !== '') {
        this.uploadShow = false
      } else {
        this.uploadShow = true
      }
      if (this.parames.channel === "IOSOfficial" || this.parames.channel === "AppStore"){
          this.uploadData.ios = true;
          this.accept = '.ipa'
        } else {
          this.uploadData.ios = false;
          this.accept = '.apk'
        }
    },
    goBtn () {
      if(this.parames.name === ''){
        this.$alert('请输入应用名称', '提示')
      } else if (this.parames.boundleId === '') {
        this.$alert('请输入boundleId', '提示')
      } else if (this.parames.channel === '') {
        this.$alert('请选择版本', '提示')
      } else if (this.parames.version === '') {
        this.$alert('请输入审核版本', '提示')
      } else {
        this.uploadShow = false
      }
    },
    beforeUpload() {
      this.uploadData.boundleId = this.parames.boundleId;
      this.uploadData.version = this.parames.version;
      this.uploadData.showName = this.parames.name;
    },
    handleSend() {
      let url = "";
      if (this.parames.id) {
        url = "tx/app/edit";
      } else {
        url = "tx/app/save";
      }
      this.$refs.parames.validate(valid => {
        if (valid) {
          var mode = "";
          if (this.moduleKeys) {
            mode = this.moduleKeys.join(",");
          }
          this.$axios
            .post(url, {
              id: this.parames.id,
              name: this.parames.name,
              url: this.parames.url,
              boundleId: this.parames.boundleId,
              channel: this.parames.boundleId,
              version: this.parames.version,
              status: this.parames.status,
              memo: this.parames.memo
            })
            .then(res => {
              if (res.code === 10200) {
                this.resetData();
                this.$router.push({ name: "app_list" });
              }
            });
        } else {
          return false;
        }
      });
    },
    changeStatus(val) {
      if (val == 0) {
        this.statusBtn = true;
        setTimeout(() => {
          this.statusBtn = false;
        }, 1000);
      }
    },
    render(id) {
      this.$axios.get("/tx/app/edit/" + id).then(res => {
        if (res.code === 10200) {
          if (id === "0") {
          } else {
            this.parames = res.data;
            this.parames.status = res.data.status + "";
          }
        }
      });
    },
    resetData(formName) {
      this.parames.url = "";
      this.parames.id = "";
      this.parames.name = "";
      this.parames.channel = "";
      this.parames.status = '0';
      this.parames.memo = "";
      this.parames.boundleId = '';
      this.parames.version = ''
      this.$refs.parames.resetFields();
    }
  },
  mounted() {
    this.parames.id = this.$route.query.id;
    if (this.parames.id) {
      this.render(this.parames.id);
    } else {
    }
    this.action = this.$axios.api + "/tx/upload/app";
  }
};
</script>
<style>
.el-checkbox + .el-checkbox {
  margin: 0;
  padding-right: 30px;
}
</style>
